package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/course/constant"
	"github.com/lab-online/internal/course/entity"
	mw "github.com/lab-online/pkg/middleware"
	"github.com/lab-online/pkg/resp"
)

type UpdateCourseReqParam struct {
	ID uint `uri:"id" binding:"required,min=1" example:"1"` // 课程ID
}

type UpdateCourseReqBody struct {
	Name        string `json:"name" binding:"required,min=1,max=20" example:"高等数学"`         // 课程名称
	Description string `json:"description" binding:"required,min=1,max=100" example:"课程描述"` // 课程描述
}

var UpdateCourseValidator = mw.NewValidator(func() *mw.ValidatorOptions {
	return &mw.ValidatorOptions{
		Params: &UpdateCourseReqParam{},
		Body:   &UpdateCourseReqBody{},
	}
})

type UpdateCourseRespBody struct {
	resp.BaseRespBody
}

// UpdateCourse 更新课程
//
//	@Summary				更新课程
//	@Description			管理员可以更新课程信息
//	@Description.markdown	UpdateCourse
//	@Tags					course
//	@Accept					json
//	@Produce				json
//	@Security				bearer
//	@Param					Authorization	header		string				true	"`admin`"
//	@Param					id				path		uint				true	"课程ID"
//	@Param					course			body		UpdateCourseReqBody	true	"更新信息"
//	@Success				201				{object}	UpdateCourseRespBody
//	@Failure				400				{object}	resp.FailedRespBodyString
//	@Failure				403				{object}	resp.FailedRespBodyString
//	@Failure				500				{object}	resp.FailedRespBodyString
//	@x-apifox-status		"developing"
//	@Router					/v1/courses/:id	[put]
func (h *Handler) UpdateCourse(c *gin.Context) {
	id := c.MustGet(mw.KeyParams).(*UpdateCourseReqParam).ID
	body := c.MustGet(mw.KeyBody).(*UpdateCourseReqBody)

	courseEntity := entity.NewCourseEntity(
		entity.WithID(id),
		entity.WithName(body.Name),
		entity.WithDescription(body.Description),
	)

	if err := h.domain.UpdateCourse(courseEntity); err != nil {
		resp.ErrorHandler(c, err)
	} else {
		resp.Success[any](c, constant.CodeUpdateCourseSuccess)
	}
}
